package handler

import (
	"2302a/tiktok_business/api/request"
	"2302a/tiktok_business/api/resp"
	"2302a/tiktok_business/global"
	"2302a/tiktok_business/internal/dao"
	"2302a/tiktok_business/internal/model"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"time"
)

func AddMessage(c *gin.Context) {
	var req request.AddMessage
	err := c.ShouldBind(&req)
	if err != nil {
		resp.Error(c, http.StatusUnauthorized, "参数信息错误", err.Error())
		return
	}
	parse, _ := time.Parse(time.DateTime, req.SendTime)
	messages := model.Messages{
		ReceiverId: req.ReceiverId,
		SenderId:   req.SenderId,
		MType:      1,
		Content:    req.Content,
		SendTime:   parse,
		IsRead:     req.IsRead,
		IsExpired:  req.IsExpired,
		CreatedAt:  time.Time{},
		UpdatedAt:  time.Time{},
		DeletedAt:  gorm.DeletedAt{},
	}
	dao.Create(&messages)
	resp.Success(c, "消息互动成功", true)
}

func UnreadMessage(c *gin.Context) {
	var req request.UnreadMessage
	err := c.ShouldBind(&req)
	if err != nil {
		resp.Error(c, http.StatusUnauthorized, "参数信息错误", err.Error())
		return
	}
	var message model.Messages
	global.DB.Find(&message)
	if message.MessageId == 0 {
		resp.Error(c, http.StatusUnauthorized, "信息错误", false)
		return
	}
	dao.Updates(&model.Messages{MessageId: req.MessageId, IsRead: message.IsRead, IsExpired: req.IsExpired})
	resp.Success(c, "统计未读总数成功", message.IsRead)
}
